package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.Client;
import com.neusoft.SPNursingHome.po.MealCalendarWithFood;
import com.neusoft.SPNursingHome.po.MealSet;
import com.neusoft.SPNursingHome.po.MealSetWithClient;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MealSetDao extends BaseMapper<MealSet> {
    /**
     * 分页查询所有套餐，并关联客户和护理等级信息
     */
    @Select("""
        SELECT
          ms.set_id,
          ms.client_like,
          ms.notice,
          c.client_id      AS clientId,
          c.client_name      AS clientName,
          c.client_type      AS clientType,
          c.client_age       AS clientAge,
          c.care_level_id    AS careLevelId,
          cl.care_level_name AS careLevelName
        FROM client c
        LEFT JOIN meal_set ms
          ON ms.client_id = c.client_id
        LEFT JOIN care_level cl
          ON c.care_level_id = cl.care_level_id
        """)
    IPage<MealSetWithClient> selectWithClientName(IPage<MealSetWithClient> page);

    /**
     * 根据客户姓名模糊分页查询套餐，并关联客户和护理等级信息
     */
    @Select("""
        SELECT
          ms.set_id,
          ms.client_like,
          ms.notice,
          c.client_id      AS clientId,
          c.client_id      AS clientId,
          c.client_name      AS clientName,
          c.client_type      AS clientType,
          c.client_age       AS clientAge,
          c.care_level_id    AS careLevelId,
          cl.care_level_name AS careLevelName
        FROM client c
        JOIN meal_set ms
          ON ms.client_id = c.client_id
        LEFT JOIN care_level cl
          ON c.care_level_id = cl.care_level_id
        WHERE c.client_name LIKE CONCAT('%', #{name}, '%')
        ORDER BY ms.set_id
        """)
    IPage<MealSetWithClient> selectWithClientNameByName(IPage<MealSetWithClient> page, @Param("name") String name);

    @Select("""
        SELECT *
          FROM client c
         WHERE NOT EXISTS (
             SELECT 1
               FROM meal_set m
              WHERE m.client_id = c.client_id
         )
        """)
    List<Client> selectClientsWithoutMealSet();

    @Select("""
        SELECT *
          FROM client c
         WHERE NOT EXISTS (
             SELECT 1
               FROM meal_set m
              WHERE m.client_id = c.client_id
         )
           AND c.client_name LIKE CONCAT('%', #{name}, '%')
        """)
    List<Client> listClientsWithoutMealSet(String name);

    // 全量查询增加健康管家过滤
    @Select("""
        <script>
        SELECT
          ms.set_id,
          ms.client_like,
          ms.notice,
          c.client_id AS clientId,
          c.client_name AS clientName,
          c.client_type AS clientType,
          c.client_age AS clientAge,
          c.care_level_id AS careLevelId,
          cl.care_level_name AS careLevelName
        FROM client c
        LEFT JOIN meal_set ms ON ms.client_id = c.client_id
        LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id
        <where>
          <if test="careAdminId != null">
            c.care_admin_id = #{careAdminId}
          </if>
        </where>
        </script>
        """)
    IPage<MealSetWithClient> selectWithClientName1(
            IPage<MealSetWithClient> page,
            @Param("careAdminId") Integer careAdminId);

    // 按姓名查询增加健康管家过滤
    @Select("""
        <script>
        SELECT
          ms.set_id,
          ms.client_like,
          ms.notice,
          c.client_id AS clientId,
          c.client_name AS clientName,
          c.client_type AS clientType,
          c.client_age AS clientAge,
          c.care_level_id AS careLevelId,
          cl.care_level_name AS careLevelName
        FROM client c
        JOIN meal_set ms ON ms.client_id = c.client_id
        LEFT JOIN care_level cl ON c.care_level_id = cl.care_level_id
        WHERE c.client_name LIKE CONCAT('%', #{name}, '%')
        <if test="careAdminId != null">
          AND c.care_admin_id = #{careAdminId}
        </if>
        ORDER BY ms.set_id
        </script>
        """)
    IPage<MealSetWithClient> selectWithClientNameByName1(
            IPage<MealSetWithClient> page,
            @Param("name") String name,
            @Param("careAdminId") Integer careAdminId);
}
